(function() {
    var app = {};

    //分页
    function loadPageList($container, curPage, size) {
        $container.children().hide();
        for (var i = (curPage - 1) * size ; i < curPage * size; i++) {
            var $item = $container.children().eq(i)
            $item.show();
            if ($item.find('img').length) {
                $item.find('img').attr('src', $item.find('img').data('src'));
            }
        }
        $container.trigger('loadComplete');
    }
    app.pagination = function($container, option) {
        if (option.itemsTotal > option.perPageLen) {
            $('#pagination').pagination({
                itemsTotal: option.itemsTotal,
                perPageLen: option.perPageLen,
                midPageLen: 3,
                sidePageLen: 3,
                isShowFirstLastPage: false,
                prevPageTxt: '<',
                nextPageTxt: '>',
                showInput: true,
                onReady: function(curPage, size) {
                    loadPageList($container, curPage, size);
                },
                onClick: function(curPage, size) {
                    loadPageList($container, curPage, size);
                },
                onSubmit: function(curPage, size) {
                    loadPageList($container, curPage, size);
                }
            });
        }
    };

    app.video = {
        setting: {
            container: '',
            url: '',
            poster: '',
            width: 800,
            height: 500,
            loop: true,
            autoplay: true,
            controls: true,
            fullscreen: false,
            muted: false
        },

        create: function(setting){
            var setting = $.extend({}, common.video.setting, setting),
            $player = $(setting.container),
            isOldIE = common.support.ie() && common.support.ie() < 9;

            /* 如果是初次创建视频,则拼接html模板并插入到页面，并挂上关闭弹窗事件 */
            if($player.length && setting.url){
                var str = '';

                if(!isOldIE){
                    str += '<video src="' + setting.url + '" width="' + setting.width + '" height="' + setting.height  + '"' + (setting.autoplay ? ' autoplay="autoplay"' : '') + (setting.loop ? ' loop="loop"' : '') + (setting.controls ? ' controls="controls"' : '') + (setting.muted ? ' muted="true"' : '') + (setting.poster ? ' poster="' + setting.poster  + '"' : '') +'>你的浏览器不支持video标签</video>';
                }else{
                    str += '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-ms-wmp" width="' + setting.width + '" height="' + setting.height + '">';
                    str +=      '<param name="url" value="' + setting.url + '">';
                    str +=      '<param name="autoStart" value="' + setting.autoplay + '">';
                    str +=      '<param name="loop" value="' + setting.loop + '">';
                    str +=      setting.loop ? '<param name="playCount" value="1000000000">' : '';
                    str +=      '<param name="showControls" value="false">';
                    str +=      '<param name="quality" value="high">';
                    str +=      '<param name="align" value="middle">';
                    str +=      '<param name="allowScriptAccess" value="always">';
                    str +=      '<param name="allowFullScreen" value="true">';
                    str +=      '<param name="windowlessVideo" value="true">';
                    str +=      '<param name="stretchToFit" value="true">';
                    str +=      setting.muted ? '<param name="volume" value="0">' : '<param name="volume" value="100">';
                    str +=      setting.controls ? '' : '<param name="uiMode" value="none">';
                    str += '</object>';
                }

                $player.html(str);
            }
        },

        /* 视频播放/暂停切换 */
        control: function($video, status){
            $video = $video[0];

            if($video.tagName.toLocaleLowerCase() === 'video'){
                switch (status){
                    case 'play':
                    $video.play();
                    break;

                    case 'pause':
                    $video.pause();
                    break;
                }
            }else{
                switch (status){
                    case 'play':
                    $video.controls.play();
                    break;

                    case 'pause':
                    $video.controls.pause();
                    break;
                }
            }
        }
    };

    

    window.app = app;
})();